package com.njupt.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njupt.common.pojo.EasyUIDataGridResult;
import com.njupt.common.pojo.IDUtils;
import com.njupt.common.pojo.JsonUtils;
import com.njupt.common.pojo.TaotaoResult;

import com.njupt.mapper.TbItemDescMapper;
import com.njupt.mapper.TbItemMapper;
import com.njupt.mapper.TbItemParamMapper;
import com.njupt.pojo.TbItem;
import com.njupt.pojo.TbItemDesc;
import com.njupt.pojo.TbItemExample;
import com.njupt.pojo.TbItemParam;
import com.njupt.service.ItemService;
import njupt.jedis.JedisClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.*;
import java.util.Date;
import java.util.List;


/**
 * @Author: XJL
 * @Description:
 * @Date: Create in 15:38 2018/5/24
 * @Modified By:
 **/

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private TbItemMapper tbItemMapper;

    @Autowired
    private TbItemDescMapper tbItemDescMapper;

    @Autowired
    private TbItemParamMapper tbItemParamMapper;

    @Autowired
    private JedisClient jedisClient;

    @Autowired
    private JmsTemplate jmsTemplate;

    @Resource(name = "itemAddTopic")
    private Destination destination;

//    @Value("${BASE}")
//    private String BASE;

    @Value("${ITEM_INFO_PRE}")
    private String ITEM_INFO_PRE;

    @Value("${EXPIRE_TIME}")
    private Integer EXPIRE_TIME;

    @Override
    public TbItem getItemById(long itemId) {
        //查询缓存
        try {
            String tbItemJson = jedisClient.get(ITEM_INFO_PRE+itemId+"BASE");
            if (StringUtils.isNotBlank(tbItemJson)){
                return JsonUtils.jsonToPojo(tbItemJson,TbItem.class);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //查询数据库
        TbItem tbItem = tbItemMapper.selectByPrimaryKey(itemId);

        //设置缓存
        try {
            jedisClient.set(ITEM_INFO_PRE+":"+itemId+":"+"BASE", JsonUtils.objectToJson(tbItem));
            //设置key的过期时间
            jedisClient.expire(ITEM_INFO_PRE+":"+itemId+":"+"BASE",EXPIRE_TIME);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tbItem;
    }

    @Override
    public EasyUIDataGridResult getItemList(int page, int rows) {
        PageHelper.startPage(page,rows);
        TbItemExample example = new TbItemExample();
        List<TbItem> list = tbItemMapper.selectByExample(example);
        EasyUIDataGridResult result = new EasyUIDataGridResult();
        if (list!=null){

            PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(list);
            result.setRows(list);
            result.setTotal(pageInfo.getTotal());
        }

        return result;
    }

    @Override
    public TaotaoResult addItem(TbItem item, String itemDesc) {

        //生成商品id
        final long itemId = IDUtils.genItemId();

        System.out.println("itemId"+itemId);
        item.setId(itemId);
        ////商品状态，1-正常，2-下架，3-删除
        item.setStatus((byte) 1);
        item.setCreated(new Date());
        item.setUpdated(new Date());
        tbItemMapper.insert(item);

        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemId(itemId);
        tbItemDesc.setItemDesc(itemDesc);
        tbItemDesc.setCreated(new Date());
        tbItemDesc.setUpdated(new Date());
        tbItemDescMapper.insert(tbItemDesc);
        //向ActiveMq发送商品信息
        jmsTemplate.send(destination, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                System.out.println("++++++++++发送activeMq消息");
                TextMessage textMessage = session.createTextMessage(itemId+"");
                return textMessage;
            }
        });
        //返回结果

        return TaotaoResult.ok();
    }

    @Override
    public TaotaoResult deleteItem(long itemId) {

        Integer num = tbItemMapper.deleteByPrimaryKey(itemId);

        if (num!=null||num!=0){
            return TaotaoResult.ok();
        }

        return TaotaoResult.build(500,"数据库错误，删除失败！");
    }

    @Override
    public TaotaoResult inshellItem(long itemId) {
        TbItem tbItem = tbItemMapper.selectByPrimaryKey(itemId);
        if (tbItem!=null){
            //商品状态，1-正常，2-下架，3-删除
            tbItem.setStatus((byte) 1);
            Integer num = tbItemMapper.updateByPrimaryKey(tbItem);
            if (num!=0&&num!=null){
                return TaotaoResult.ok();
            }
        }

        return TaotaoResult.build(500,"数据库错误！");
    }

    @Override
    public TaotaoResult unshellItem(long itemId) {

        TbItem tbItem = tbItemMapper.selectByPrimaryKey(itemId);
        if (tbItem!=null){
            tbItem.setStatus((byte) 2);
            Integer num = tbItemMapper.updateByPrimaryKey(tbItem);
            if (num!=0&&num!=null){
                return TaotaoResult.ok();
            }
        }
        return TaotaoResult.build(500, "\u6570\u636e\u5e93\u5f02\u5e38\uff01");
    }

    @Override
    public TaotaoResult updateItem(TbItem tbItem, String itemDesc, TbItemParam tbItemParam) {
        TbItem tbItem1 = tbItemMapper.selectByPrimaryKey(tbItem.getId());
        tbItem.setUpdated(new Date());
        tbItem.setCreated(tbItem1.getCreated());
        tbItem.setStatus((byte) 1);
        tbItemMapper.updateByPrimaryKey(tbItem);
       TbItemDesc tbItemDesc = tbItemDescMapper.selectByPrimaryKey(tbItem.getId());
       tbItemDesc.setItemDesc(itemDesc);
       tbItemDesc.setUpdated(new Date());
       tbItemDescMapper.updateByPrimaryKeySelective(tbItemDesc);
       tbItemParamMapper.updateByPrimaryKey(tbItemParam);

        return TaotaoResult.ok();
    }

    @Override
    public TbItemDesc getItemDescById(long itemId) {
        //查询缓存
        try {
           String descJson = jedisClient.get(ITEM_INFO_PRE+":"+itemId+":"+"DESC");
           if (StringUtils.isNotBlank(descJson)){
               return JsonUtils.jsonToPojo(descJson,TbItemDesc.class);
           }
        } catch (Exception e) {
            e.printStackTrace();
        }

        TbItemDesc  itemDesc = tbItemDescMapper.selectByPrimaryKey(itemId);

        try {
            //查询缓存
            jedisClient.set(ITEM_INFO_PRE+":"+itemId+":"+"DESC",JsonUtils.objectToJson(itemDesc));
            //设置过期时间
            jedisClient.expire(ITEM_INFO_PRE+":"+itemId+":"+"DESC",EXPIRE_TIME);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return itemDesc;
    }
}
